package com.wavehi.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author WaveHi
 * @Date 17/9/2021 下午 3:27
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Basic Servlet
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置客户端的字符集
        req.setCharacterEncoding("UTF-8");
        //设置服务器端的字符集
        resp.setContentType("UTF-8");
        //获取action的参数值
        String action = req.getParameter("action");
        System.out.println(action);
        //判断action的值
        boolean isCorrect = action != null && !"".equals(action.trim());
        //处理请求
        if(isCorrect) {
            try {
                //获取指定方法
                Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
                method.setAccessible(true);
                System.out.println(method);
                //执行方法
                method.invoke(this, req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
